package com.qdxwx.manage.controller.course;

import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.course.CourseNodeService;
import com.qdxwx.data.service.course.CourseService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseNode;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 知识点的基本信息Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/course/node")
public class CourseNodeController extends BaseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseNodeService courseNodeService;

    @GetMapping("/list.html")
    public String node(HttpServletRequest request , ModelMap mmap) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        String sellType = request.getParameter("sellType");
        if (courseId < 1 || TKString.isEmpty(sellType)) return error500;
        Course course = courseService.get(courseId);
        if (course == null) {
            return error404;
        }
        mmap.put("course", course);
        mmap.put("courseId", courseId);
        mmap.put("sellType", sellType);
        return "course/node-list";
    }

    /**
     * 查询知识点的基本信息树列表
     */
    @Permissions("course:list")
    @PostMapping("/list.json")
    @ResponseBody
    public List<CourseNode> list(HttpServletRequest request) {
        int courseId = TKConvert.toInteger(request.getParameter("courseId"));
        if(courseId < 1) return new ArrayList<>();
        String name = request.getParameter("name");
        CourseNode courseNode = new CourseNode();
        courseNode.setCourseId(courseId);
        courseNode.setName(name);
        return courseNodeService.getList(courseNode, null);
    }
}
